home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / VideoToolbox 96.06.15 / VideoToolboxSources / OpenDataFiles.c < prev    next >
Text File  |  1994-05-28  |  3KB  |  81 lines

  1. /*
  2. OpenDataFile.c
  3. This is something I found myself writing repeatedly. It asks the user for a
  4. parameter file with extension ".in" to be used for input (to control an experiment),
  5. and opens it as the input stream *inPtr (with default stdin). Then it opens a new output
  6. file with a unique name formed by appending a time stamp onto the input file name.
  7. If there's no input file then the user is asked to supply a base file name for the
  8. output file, to which the extension is added. If the total file name would be more than
  9. 31 characters, the maximum allowed on the Mac, then the base name is truncated. This
  10. guarantees that the extension will still be unique. 
  11.  
  12. Finally the output file type is set to Excel.
  13.  
  14. Since the time stamp advances only once per second, I added a check to make sure
  15. that the proposed new output file doesn't already exist. The time stamp is advanced
  16. as many seconds as necessary to create a unique file name.
  17.  
  18. The inName string allocation should be at least 32 bytes. I suggest the outName 
  19. string allocation be at least 40 bytes.
  20.  
  21. HISTORY:
  22. 9/14/90    dgp    extracted it from UserOpen.c, and cleaned it up for general use.
  23. 9/15/90    dgp    added handling of special cases: filename too long, file 
  24.             already exists.
  25. 10/11/90 dgp Now you can supply NULL instead of any of the arguments, causing the
  26.             corresponding questions (input & output file names) to be skipped.
  27. 8/24/91    dgp    Made compatible with THINK C 5.0.
  28. 10/23/92 dgp Treat single-character filename same as longer names.
  29. 1/25/93 dgp removed obsolete support for THINK C 4.
  30. */
  31. #include "VideoToolbox.h"
  32.  
  33. unsigned long OpenDataFiles(FILE **inPtr,FILE **outPtr,char *inName,char *outName)
  34. {
  35.     unsigned long seconds;
  36.     char string[64]="";
  37.     FILE *in;
  38.     
  39.     GetDateTime(&seconds);
  40.     in=NULL;
  41.     if(inPtr!=NULL && inName!=NULL){
  42.         printf("Please enter input filename. \n"
  43.             "Extension .in will be added. Just cr for none:");
  44.         gets(inName);
  45.         strcpy(string,inName);
  46.         if(strlen(inName)>0){
  47.             strncat(inName,".in",5);
  48.             in=fopen(inName,"r");
  49.             if(in==NULL)PrintfExit("Sorry, can't find \"%s\".\007\n",inName);
  50.         }
  51.     }
  52.     if(in==NULL)in=stdin;
  53.     if(inPtr!=NULL)*inPtr=in;
  54.     if(outPtr!=NULL && outName!=NULL){
  55.         *outPtr=NULL;
  56.         if(strlen(string)==0){
  57.             printf("Please enter output filename. \n"
  58.                 "Extension .%s.data will be added.\n"
  59.                 "Just cr for none:",DatedString(seconds));
  60.             fgets(string,sizeof(string)-1,in);
  61.             if(strlen(string)>0)string[strlen(string)-1]=0;        /* Remove trailing cr */
  62.         }
  63.         if(strlen(string)>0){
  64.             goto A;
  65.             do{
  66.                 if(strlen(string)>0)string[strlen(string)-1]=0;        /* Remove last char */
  67.                 A: sprintf(outName,"%s.%s.data",string,DatedString(seconds));
  68.             } while(strlen(outName)>31);        /* max length for Mac filename */
  69.             *outPtr=fopen(outName,"r");
  70.             if(*outPtr!=NULL){
  71.                 fclose(*outPtr);
  72.                 seconds++;
  73.                 goto A;
  74.             }
  75.             *outPtr=fopen(outName,"w");
  76.             if(*outPtr==NULL)PrintfExit("Sorry, can't create \"%s\".\007\n",outName);
  77.         }
  78.         if(*outPtr!=NULL) SetFileInfo(outName,'TEXT','KAHL');
  79.     }
  80.     return seconds;
  81. }